package com.zhulanhua.blogs.config;

import com.zhulanhua.blogs.Utils.FileUtils;
import com.zhulanhua.blogs.Utils.PathUtils;
import com.zhulanhua.blogs.filter.AuthHandlerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
public class WebMvcConfig  implements WebMvcConfigurer {

    @Autowired
    AuthHandlerInterceptor authHandlerInterceptor;

    @Autowired
    PathUtils pathUtils;

    /**
     * 拦截器配置
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authHandlerInterceptor)
                .addPathPatterns("/api/**")
                .excludePathPatterns(
                        "/api/comments/praise",
                        "/api/comments/save",
                        "/api/friends/save",
                        "/api/users/login",
                        "/api/users/info",
                        "/api/users/password",
                        "/api/users/md5"
                );
    }

    /**
     * 添加静态资源文件，外部可以直接访问地址
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 创建项目需要的文件夹
        FileUtils.files(pathUtils.path, new String[]{pathUtils.filePath, pathUtils.imagesPath});

        registry.addResourceHandler(pathUtils.resourcesPath + "/**")
                .addResourceLocations("file:" + pathUtils.path.replace("\\","/") + "/");
    }
}
